package com.zzedu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zzedu.common.annotation.Log;
import com.zzedu.common.core.controller.BaseController;
import com.zzedu.common.core.domain.AjaxResult;
import com.zzedu.common.core.page.TableDataInfo;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.common.utils.ServletUtils;
import com.zzedu.common.utils.poi.ExcelUtil;
import com.zzedu.domain.ZzTimetable;
import com.zzedu.dto.ZzTimetableDTO;
import com.zzedu.params.ZzClassesParams;
import com.zzedu.service.IZzTimetableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * (教育-校区)班级课程Controller
 *
 * @author Mr.Wang
 * @date 2025-06-05
 */
@RestController
@RequestMapping("/school/timetable")
public class ZzTimetableController extends BaseController {
    @Autowired
    private IZzTimetableService zzTimetableService;

    /**
     * 查询(教育-校区)班级课程列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:timetable:query')")
    @RequestMapping("/list")
    public TableDataInfo list(@RequestBody ZzTimetableDTO zzTimetable) {
        IPage<ZzTimetable> page = zzTimetableService.page(zzTimetable.getPageDTO(), zzTimetableService.buildQuery(zzTimetable));
        return getDataTable(page, zzTimetableService.convertVO(page.getRecords()));
    }

    /**
     * 导出(教育-校区)班级课程列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:timetable:export')")
    @Log(title = "(教育-校区)班级课程", businessType = BusinessType.EXPORT)
    @RequestMapping("/export")
    public void export(ZzTimetableDTO zzTimetable) {
        List<ZzTimetable> list = zzTimetableService.list(zzTimetableService.buildQuery(zzTimetable));
        ExcelUtil<ZzTimetable> util = new ExcelUtil<ZzTimetable>(ZzTimetable.class);
        util.exportExcel(ServletUtils.getResponse(), list, "(教育-校区)班级课程数据");
    }


    /**
     * 新增保存(教育-校区)班级课程
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:timetable:add')")
    @Log(title = "(教育-校区)班级课程", businessType = BusinessType.INSERT)
    @RequestMapping("/add")
    public AjaxResult addSave(@Validated @RequestBody ZzTimetable zzTimetable) {
        return toAjax(zzTimetableService.save(zzTimetable));
    }


    /**
     * 获取(教育-校区)班级课程详细信息
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:timetable:query')")
    @RequestMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(zzTimetableService.getById(id));
    }


    /**
     * 修改保存(教育-校区)班级课程
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:timetable:edit')")
    @Log(title = "(教育-校区)班级课程", businessType = BusinessType.UPDATE)
    @RequestMapping("/edit")
    public AjaxResult editSave(@Validated @RequestBody ZzTimetable zzTimetable) {
        LocalDateTime studyDate = zzTimetable.getStudyDate();
        String[] hours = zzTimetable.getStartDate().split(":");
        studyDate = studyDate.withHour(Integer.parseInt(hours[0]));
        studyDate = studyDate.withMinute(Integer.parseInt(hours[1]));
        zzTimetable.setStudyDate(studyDate);
        return toAjax(zzTimetableService.updateById(zzTimetable));
    }

    /**
     * 删除(教育-校区)班级课程
     */
    @PreAuthorize("@permission.hasAnyPermissions('school:timetable:remove')")
    @Log(title = "(教育-校区)班级课程", businessType = BusinessType.DELETE)
    @RequestMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(zzTimetableService.removeBatchByIds(ids));
    }


    /**
     * 课表详情
     *
     * @param zzClassesParams
     * @return
     */
    @RequestMapping("/buildTimetable")
    public TableDataInfo buildTimetable(@RequestBody ZzClassesParams zzClassesParams) {
        return getDataTable(zzTimetableService.buildTimetable(zzClassesParams));
    }
}
